<style scoped>
</style>

<template>
    <div>
        <Modal
            title="发送通知消息"
            :value="value"
            @on-ok="save"
            @on-visible-change="visibleChange"
            width="500"
        >
            <Form
                ref="msgForm"
                label-position="top"
                :rules="msgRule"
                :model="message"
            >
                <div style="padding-bottom: 10px;">接收： {{message.userName}}</div>
                <FormItem label="消息标题" prop="title">
                    <Input v-model="message.title" placeholder="请输入消息标题"></Input>
                </FormItem>
                <FormItem label="消息" prop="msg">
                    <Input v-model="message.msg" type="textarea" placeholder="请输入消息内容"></Input>
                </FormItem>
            </Form>
            <div slot="footer">
                <Button @click="cancel">{{ L("Cancel") }}</Button>
                <Button @click="save" type="primary">{{ L("OK") }}</Button>
            </div>
        </Modal>
    </div>
</template>

<script lang="ts">
import { Component, Vue, Inject, Prop, Watch } from "vue-property-decorator";
import Util from "@/lib/util";
import Ajax from "@/lib/ajax"
import AbpBase from "@/lib/abpbase";
class Msg {
    userName: string = '';
    title: string = '';
    msg: string = '';
    userId: number=0;
}
@Component({
    components: {},
})
export default class extends AbpBase {
    @Prop({ type: Boolean, default: false }) value: boolean;

    message: Msg = new Msg();

    save() {
        (this.$refs.msgForm as any).validate(async (valid: boolean) => {
            let url = '';
            if (!this.message.userId) {
                url = '/api/services/app/Notification/SendCommonMessageToAllUser';
            } else {
                url = '/api/services/app/Notification/SendCommonMessageToUser';
            }
            if (valid) {
                await Ajax.post(url,this.message);
                (this.$refs.msgForm as any).resetFields();
                this.$emit("save-success");
                this.$emit("input", false);
            }
        });
    }
    cancel() {
        (this.$refs.msgForm as any).resetFields();
        this.$emit("input", false);
    }
    visibleChange(value: boolean) {
        if (!value) {
            this.$emit("input", value);
        } else {
            let user = this.$store.state.user.editUser

            if (!user) {
                this.message.userId = 0;
                this.message.userName = "所有人";
            } else {
                this.message.userId = user.id;
                this.message.userName = user.name;
            }
        }
    }
    msgRule={
        msg:[
            {
                required: true,
                message: "请输入要发送的消息",
                trigger: 'blur'
            }
        ],
        title:[
            {
                required: true,
                message: "请输入消息标题",
                trigger: 'blur'
            }
        ],
    }
}
</script>